local data = require('scripts/core_wedder/data/tuning').IDENTITY
local MAXLEVEL = require('scripts/core_wedder/data/tuning').MAXLEVEL

-- 按这个顺序
local sequence = {
    'spider',
    'spider_warrior',
    'spider_healer',
    'spider_hider',
    'spider_spitter',
    'spider_dropper',
    'spider_water',
    'spider_moon'
}

-- 表头信息
local TITLE = {
    cn = {
        {'初始HP','lime'},
        {'成长HP','lime'},
        {'HP区间 <br> 最低-最高','white'},
        {'初始伤害','crimson'},
        {'成长伤害','crimson'},
        {'伤害区间 <br> 最低-最高','white'},
        {'受伤倍率','goldenrod'},
        {'每级受伤','goldenrod'},
        {'成长概率','goldenrod'},
        {'受伤区间 <br> 最低-平均-最高','white'},
        {'攻速','purple'},
        {'成长因子','purple'},
        {'成长概率','purple'},
        {'攻速区间 <br> 最低-平均-最高','white'},
        {'移速','aqua'},
        {'每级成长','aqua'},
        {'成长概率','aqua'},
        {'移速区间 <br> 最低-平均-最高','white'},
    }
}

---制作表头
---@param lang string|'cn'|'en'
---@return string
local function gen_title(lang)
    local res = '| VIEW |'
    local _res = '| - |'
    local span = '<span style="color: %s;">%s</span>'
    local function format_span(content,color)
        return string.format(span, color, content)
    end
    for i,v in ipairs(TITLE[lang]) do
        res = res .. ' ' .. format_span(v[1], v[2]) .. ' |'
        _res = _res .. ' - |'
    end
    print(res..'\n'.._res)
    return res..'\n'.._res
end

---保留2位小数
---@param num number
---@return string
local function fnum(num)
    return string.format('%.2f', num)
end

---生成对应蜘蛛的单行表格
---@param name string
---@return string
local function gen_kind(name)
    local hp = data[name].hp
    local res = string.format('| ![%s](https://gitee.com/b1inkie/imgbed/raw/master/dst/invimgs/%s.png) <br> %s |',name,name,hp)
    res = res .. string.format(' %s - %s |',fnum(data[name].hp_start[1]),fnum(data[name].hp_start[2]))
    res = res .. string.format(' %s - %s |',fnum(data[name].hp_glow[1]),fnum(data[name].hp_glow[2]))
    local hp_min,hp_max = fnum(hp * data[name].hp_start[1] + MAXLEVEL * data[name].hp_glow[1]),fnum(hp * data[name].hp_start[2] + MAXLEVEL * data[name].hp_glow[2])
    res = res .. string.format(' %s - %s |',hp_min,hp_max)
    res = res .. string.format(' %s - %s |',fnum(data[name].dmg_start[1]),fnum(data[name].dmg_start[2]))
    res = res .. string.format(' %s - %s |',fnum(data[name].dmg_grow[1]),fnum(data[name].dmg_grow[2]))
    local dmg_min,dmg_max = fnum(data[name].dmg_start[1] + MAXLEVEL * data[name].dmg_grow[1]),fnum(data[name].dmg_start[2] + MAXLEVEL * data[name].dmg_grow[2])
    res = res .. string.format(' %s - %s |',dmg_min,dmg_max)
    res = res .. string.format(' %s |',fnum(data[name].wound))
    res = res .. string.format(' %s |',fnum(data[name].wound_per))
    res = res .. string.format(' %s |',fnum(data[name].wound_grow_chance))
    local wound_min = data[name].wound
    local wound_mid = wound_min * (data[name].wound_per ^ (MAXLEVEL*data[name].wound_grow_chance))
    local wound_max = wound_min * (data[name].wound_per ^ MAXLEVEL)
    res = res .. string.format(' %s - %s - %s |',fnum(wound_min),fnum(wound_mid),fnum(wound_max))

    res = res .. string.format(' %s |',fnum(data[name].period))
    res = res .. string.format(' %s |',fnum(data[name].period_grow_factor))
    res = res .. string.format(' %s |',fnum(data[name].period_grow_chance))
    local period_min = data[name].period
    local period_mid = period_min + data[name].period_grow_factor * math.log(MAXLEVEL*data[name].period_grow_chance+1)
    local period_max = period_min + data[name].period_grow_factor * math.log(MAXLEVEL+1)
    res = res .. string.format(' %s - %s - %s |',fnum(period_min),fnum(period_mid),fnum(period_max))

    res = res .. string.format(' %s |',fnum(data[name].speed))
    res = res .. string.format(' %s |',fnum(data[name].speed_per))
    res = res .. string.format(' %s |',fnum(data[name].speed_grow_chance))
    local speed_min = data[name].speed
    local speed_mid = speed_min + (data[name].speed_per * MAXLEVEL * data[name].speed_grow_chance)
    local speed_max = speed_min + (data[name].speed_per * MAXLEVEL )
    res = res .. string.format(' %s - %s - %s |',fnum(speed_min),fnum(speed_mid),fnum(speed_max))
    return res
end

---制作表格
---@param lang string|'cn'|'en'
---@return string
local function gen_format(lang)
    local res = gen_title(lang) .. '\n'
    for _,name in ipairs(sequence) do
        res = res .. gen_kind(name) .. '\n'
    end
    return res
end

---生成附录表格md
---@param path string
---@param content string
local function gen_file(path,content)
    local file = io.open(path, "w")
    if file then
        file:write(content)
        file:close()
    end
end

local FILE_CONTENT = [[
## 附录

种族值&个体值:

- 初始HP: 为种族最大生命值的的 多少 到 多少 倍
- 成长HP: + `n` * 等级
- 伤害为线性增长
- 减伤为概率增长,每次升级都有概率将 每级受伤 * 种族初始受伤倍率
- 攻速为概率增长,成长函数: 种族初始 + 成长因子 * math.log(等级 + 1) 
- 移速为概率增长,每次升级都有概率 + 每级成长 的移速
]]

FILE_CONTENT = FILE_CONTENT .. '\n' .. gen_format('cn')

gen_file('DETAILS/indentiy.md',FILE_CONTENT)